package com.cheetah.start.common.config;

import cn.dev33.satoken.stp.StpInterface;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.thymeleaf.dialect.SaTokenDialect;
import com.cheetah.start.module.system.service.ISysRoleAdminService;
import com.cheetah.start.module.system.service.ISysRoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * @author Feel
 * @date 2021/10/18 15:34
 * @Description:
 */
@Configuration
public class SaTokenConfig implements StpInterface {

    @Autowired
    private ISysRoleMenuService roleMenuService;
    @Autowired
    private ISysRoleAdminService roleAdminService;

    /**
     * Sa-Token 标签方言 (Thymeleaf版)
     *
     * @return {@link SaTokenDialect}
     */
    @Bean
    public SaTokenDialect getSaTokenDialect() {
        return new SaTokenDialect();
    }

    @Override
    public List<String> getPermissionList(Object o, String s) {
        int userId = StpUtil.getLoginIdAsInt();
        return roleMenuService.getPermissionByAdminId(userId);
    }

    @Override
    public List<String> getRoleList(Object o, String s) {
        int userId = StpUtil.getLoginIdAsInt();
        return roleAdminService.getRoleCodeByAdminId(userId);
    }
}
